package com.kkd.springbootproject.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * @author 14587
 * @date 2021/1/14 19:27
 */
@EnableWebSecurity//开启安全
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override//认证
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人能访问，剩下的必须有的人不能
        http.authorizeRequests().antMatchers("/").permitAll()
                                .antMatchers("/level1/**").hasRole("vip1")
                                .antMatchers("/level2/**").hasRole("vip2");
        //如果没有权限就要去登录页,需要开启
        http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login").usernameParameter("user").passwordParameter("pwd");
        //记住我
        http.rememberMe().rememberMeParameter("remember");
        //注销登录后跳转的页面
        http.logout().logoutSuccessUrl("/");
    }

    @Override//授权//可以用passwordEncoder(new BCryptPasswordEncoder())进行一次加密
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("kkd").password("123456").roles("vip1","vip2","vip3")
                .and().withUser("dou").password("aldk").roles("vip2","vip3");
    }
}
